/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.webapp.view.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import seedpod.kb2db.ClsMap;
import seedpod.kb2db.ModelMap;
import seedpod.webapp.Seedpod;
import seedpod.webapp.view.htmlwidget.SelectInput;


/**
 * Servlet implementation class for Servlet: ModelMapViewer
 */
public class ModelMapViewer extends HttpServlet  {
	private static final long serialVersionUID = 8080363729267319034L;
	private static ModelMap _map = Seedpod.getModelMap();
	public static final String CLSNAME = "clsName";

	protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
			IOException {
		PrintWriter out = response.getWriter();
	
		boolean isConcrete = Boolean.parseBoolean( (String)request.getParameter( "isConcrete" ) );
		boolean isAbstract = Boolean.parseBoolean( (String)request.getParameter( "isAbstract" ));
		boolean isRelation = Boolean.parseBoolean( (String)request.getParameter( "isRelation" ));
		
		if ( _map == null ) {
			out.println("Sorry, the model information not available.");
			return;
		}
		
		Hashtable<String, ClsMap> clsMapCollection = _map.getClsMapCollection();
		Iterator<String> clsNameItr = clsMapCollection.keySet().iterator();
		SelectInput clsInput = new SelectInput(CLSNAME);
		clsInput.setAllowsMultiple( false );
		clsInput.setSize( 3 );
		int numSelection = 0;
		
		while (clsNameItr.hasNext()) {
			String clsName = clsNameItr.next();
			ClsMap clsMap = clsMapCollection.get( clsName );
			
			if ( !clsMap.isRdbCls() || clsMap.isSystemCls() )
				continue;
			
			if ( isRelation && clsMap.isReifiedRelation() ) {
				clsInput.addOption( clsMap.getName() );
				numSelection++;
				continue;
			}
			
			if ( clsMap.isConcrete() && isConcrete && !clsMap.isReifiedRelation() ) { 
				clsInput.addOption( clsMap.getName() );
				numSelection ++;
				continue;
			}
			
			if (!clsMap.isConcrete() && isAbstract && !clsMap.isReifiedRelation()) {
				clsInput.addOption( clsMap.getName() );
				numSelection ++;
				continue;
			}
			
		}
		
		if ( numSelection > 0 )
			out.println(clsInput.render());
		else
			out.println("No Cls selected.");
	
		
// String[] clsNames = request.getParameterValues( CLSNAME );
//
// if ( clsNames == null || clsNames.length == 0 )
// out.write( "No Cls definition requested." );
// else {
// for ( int i = 0; i < clsNames.length; i++ ) {
// ClsMap cmap = _map.getClsMap( clsNames[i] );
// if ( cmap != null )
// out.write( ClsViewer.renderClsBox( cmap ) );
// }
// }
	}

	protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
			IOException {
		doGet( request, response );
	}

	public static String getClsNameSelectionHTML() {
		Vector<String> clsNames = getClsNameCollection();
		SelectInput sin = new SelectInput( CLSNAME );

		for ( int i = 0; i < clsNames.size(); i++ ) {
			sin.addOption( clsNames.elementAt( i ) );
		}
		return sin.render();
	}

	public static Vector<String> getClsNameCollection() {
		if ( _map == null )
			return new Vector<String>();

		Collection<ClsMap> clsMaps = _map.getClsMapCollection().values();
		Vector<String> nameVector = new Vector<String>();

		for (Iterator<ClsMap> clsItr = clsMaps.iterator(); clsItr.hasNext(); ) {
			String name = clsItr.next().getName();
			nameVector.add( name );
		}
		return nameVector;
	}
}